﻿#include "FITKAbaBoundaryConditionDisplacement.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"

Interface::FITKAbaAbstractBoundaryCondition::FITKAbaBoundaryConditionType Interface::FITKAbaBoundaryConditionDisplacement::getBoundaryConditionType() const
{
    return Interface::FITKAbaAbstractBoundaryCondition::FITKAbaBoundaryConditionType::DisplacementRotation;
}

bool Interface::FITKAbaBoundaryConditionDisplacement::isModifiedInStep(int stepId) const
{
    if (m_modifiedDatas.contains(stepId))
        return true;
    return false;
}

void Interface::FITKAbaBoundaryConditionDisplacement::activeStateChanged()
{
    //在分析步中的激活状态发生变化时，激活时，不需做处理
    if (m_deactiveStepId == -1)
        return;
    //不激活时，清除后续分析步中的
    //访问当前算例的分析步管理器，获取分析步顺序id列表。
    QVector<int> stepIds = this->getStepIds();
    int index = stepIds.indexOf(m_deactiveStepId);
    if (index == -1)
        return;
    for (int i = index ; i < stepIds.count(); ++i)
    {
        int id = stepIds[i];
        if (m_modifiedDatas.contains(id))
            m_modifiedDatas.remove(id);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::copy(Core::FITKAbstractDataObject* obj)
{
    bool ok = FITKAbaAbstractBoundaryCondition::copy(obj);
    if (!ok)
        return false;
    FITKAbaBoundaryConditionDisplacement* disp = dynamic_cast<FITKAbaBoundaryConditionDisplacement*>(obj);
    if (disp == nullptr)
        return false;
    m_csysId = disp->m_csysId;
    m_analyticalFieldId = disp->m_analyticalFieldId;
    m_data = disp->m_data;
    //复制后续分析步中调整的数据
    m_modifiedDatas = disp->m_modifiedDatas;
    return true;
}

int Interface::FITKAbaBoundaryConditionDisplacement::getCsysId() const
{
    return m_csysId;
}

void Interface::FITKAbaBoundaryConditionDisplacement::setCsysId(int id)
{
    m_csysId = id;
}

int Interface::FITKAbaBoundaryConditionDisplacement::getAnalyticalFieldId() const
{
    return m_analyticalFieldId;
}

void Interface::FITKAbaBoundaryConditionDisplacement::setAnalyticalFieldId(int id)
{
    m_analyticalFieldId = id;
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getU1State(int stepId)
{
    return this->getDataInStep(stepId).getU1State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU1State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU1State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU1State(state);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getU2State(int stepId)
{
    return this->getDataInStep(stepId).getU2State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU2State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU2State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU2State(state);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getU3State(int stepId)
{
    return this->getDataInStep(stepId).getU3State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU3State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU3State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU3State(state);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getUR1State(int stepId)
{
    return this->getDataInStep(stepId).getUR1State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR1State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR1State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR1State(state);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getUR2State(int stepId)
{
    return this->getDataInStep(stepId).getUR2State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR2State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR2State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR2State(state);
    }
}

bool Interface::FITKAbaBoundaryConditionDisplacement::getUR3State(int stepId)
{
    return this->getDataInStep(stepId).getUR3State();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR3State(bool state, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR3State(state);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR3State(state);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getU1(int stepId)
{
    return this->getDataInStep(stepId).getU1();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU1(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU1(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU1(value);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getU2(int stepId)
{
    return this->getDataInStep(stepId).getU2();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU2(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU2(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU2(value);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getU3(int stepId)
{
    return this->getDataInStep(stepId).getU3();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setU3(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setU3(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setU3(value);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getUR1(int stepId)
{
    return this->getDataInStep(stepId).getUR1();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR1(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR1(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR1(value);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getUR2(int stepId)
{
    return this->getDataInStep(stepId).getUR2();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR2(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR2(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR2(value);
    }
}

double Interface::FITKAbaBoundaryConditionDisplacement::getUR3(int stepId)
{
    return this->getDataInStep(stepId).getUR3();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setUR3(double value, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setUR3(value);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setUR3(value);
    }
}

int Interface::FITKAbaBoundaryConditionDisplacement::getAmplitudeId(int stepId)
{
    return this->getDataInStep(stepId).getAmplitudeId();
}

void Interface::FITKAbaBoundaryConditionDisplacement::setAmplitudeId(int id, int stepId)
{
    if (stepId <= 0 || stepId == m_createdStepId)
        m_data.setAmplitudeId(id);
    else
    {
        if (!m_modifiedDatas.contains(stepId))
            m_modifiedDatas[stepId] = this->getDataInStep(stepId);
        m_modifiedDatas[stepId].setAmplitudeId(id);
    }
}

Interface::FITKCoordinateSystem* Interface::FITKAbaBoundaryConditionDisplacement::getCsysObj()
{
    if (m_csysId < 0) return nullptr;
    return FITKDATAREPO->getTDataByID<FITKCoordinateSystem>(m_csysId);
}

Interface::BCDispalcementRotationData Interface::FITKAbaBoundaryConditionDisplacement::getDataInStep(int stepId)
{
    if (stepId <= 0)
        return m_data;
    if (m_modifiedDatas.contains(stepId))
        return m_modifiedDatas[stepId];
    //不存在修改数据，则获取前一个被修改过的分析步数据
    QVector<int> stepIds = this->getStepIds();
    int createdStepIndex = stepIds.indexOf(m_createdStepId);
    int stepIndex = stepIds.indexOf(stepId);
    if (!(createdStepIndex >= 0 && createdStepIndex < stepIds.size()))
        return m_data;
    if (!(stepIndex >= 0 && stepIndex < stepIds.size()))
        return m_data;
    for (int i = stepIndex; i >= createdStepIndex; --i)
    {
        int id = stepIds.at(i);
        if (m_modifiedDatas.contains(id))
            return m_modifiedDatas[id];
    }
    //前面没有被修改过的分析步数据，则返回创建数据
    return m_data;
}

bool Interface::BCDispalcementRotationData::getU1State() const
{
    return m_states[0];
}

void Interface::BCDispalcementRotationData::setU1State(bool state)
{
    m_states[0] = state;
}

bool Interface::BCDispalcementRotationData::getU2State() const
{
    return m_states[1];
}

void Interface::BCDispalcementRotationData::setU2State(bool state)
{
    m_states[1] = state;
}

bool Interface::BCDispalcementRotationData::getU3State() const
{
    return m_states[2];
}

void Interface::BCDispalcementRotationData::setU3State(bool state)
{
    m_states[2] = state;
}

bool Interface::BCDispalcementRotationData::getUR1State() const
{
    return m_states[3];
}

void Interface::BCDispalcementRotationData::setUR1State(bool state)
{
    m_states[3] = state;
}

bool Interface::BCDispalcementRotationData::getUR2State() const
{
    return m_states[4];
}

void Interface::BCDispalcementRotationData::setUR2State(bool state)
{
    m_states[4] = state;
}

bool Interface::BCDispalcementRotationData::getUR3State() const
{
    return m_states[5];
}

void Interface::BCDispalcementRotationData::setUR3State(bool state)
{
    m_states[5] = state;
}

double Interface::BCDispalcementRotationData::getU1() const
{
    return m_values[0];
}

void Interface::BCDispalcementRotationData::setU1(double value)
{
    m_values[0] = value;
}

double Interface::BCDispalcementRotationData::getU2() const
{
    return m_values[1];
}

void Interface::BCDispalcementRotationData::setU2(double value)
{
    m_values[1] = value;
}

double Interface::BCDispalcementRotationData::getU3() const
{
    return m_values[2];
}

void Interface::BCDispalcementRotationData::setU3(double value)
{
    m_values[2] = value;
}

double Interface::BCDispalcementRotationData::getUR1() const
{
    return m_values[3];
}

void Interface::BCDispalcementRotationData::setUR1(double value)
{
    m_values[3] = value;
}

double Interface::BCDispalcementRotationData::getUR2() const
{
    return m_values[4];
}

void Interface::BCDispalcementRotationData::setUR2(double value)
{
    m_values[4] = value;
}

double Interface::BCDispalcementRotationData::getUR3() const
{
    return m_values[5];
}

void Interface::BCDispalcementRotationData::setUR3(double value)
{
    m_values[5] = value;
}

int Interface::BCDispalcementRotationData::getAmplitudeId() const
{
    return m_amplitudeId;
}

void Interface::BCDispalcementRotationData::setAmplitudeId(int id)
{
    m_amplitudeId = id;
}
